package com.yunduansing.demo.model.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

@ApiModel(value = "分页请求基础类")
//@Data
public class PageRequestModel {
    /**
     * 请求页码
     */
    @ApiModelProperty(value = "页码",example = "1")
    private int current;
    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页显示条数",example = "15")
    private int pageSize;
    /**
     * 查询关键字key
     */
    @ApiModelProperty(value = "查询关键字")
    private String key;
    /**
        * 批量排序关键字
        */
    @ApiModelProperty(value = "批量排序关键字")
    private String sorter;

    /**
        * 批量查询关键字
        */
    @ApiModelProperty(value = "批量查询关键字")
    private String filter;
    private Map<String,String> sorterMap;
    private Map<String,String> filterMap;
    public void setSorterMap(Map<String,String> sorterMap){
        this.sorterMap=sorterMap;
    }
    public void setFilterMap(Map<String,String> filterMap){
        this.filterMap=filterMap;
    }
    /**
     * 拿到sorter map
     */
    public Map<String,String> getSorterMap(){
        return this.sorterMap;
    }
    /**
    * 拿到filter map
    */
    public Map<String,String> getFilterMap(){
        return this.filterMap;
    }

    public void setCurrent(int index){
        this.current=index;
    }
    public int getCurrent(){
        return this.current;
    }
    public void setPageSize(int size){
        this.pageSize=size;
    }
    public int getPageSize(){
        return this.pageSize;
    }
    public void setKey(String key){
        this.key=key;
    }
    public String getKey(){
        return this.key;
    }
    public void setSorter(String sorter){
        this.sorter=sorter;
        if(!StrUtil.isBlank(sorter) && !sorter.equals("{}")){
            var obj=JSONUtil.toBean(sorter, Map.class);
            var objMap=(HashMap<String,String>)obj;
            Map<String,String> map=new HashMap<String,String>();
            for(var item:objMap.entrySet()){
                if(!StrUtil.isAllBlank(item.getKey(),item.getValue())){
                    var key=StrUtil.toSymbolCase(item.getKey().replace("'", "").replace(" ", ""), '_');
                    map.put(key, item.getValue().replace("'", "").replace(" ", ""));
                }
            }
            this.setSorterMap(map);
        }
    }
    public String getSorter(){
        return this.sorter;
    }
    public void setFilter(String filter){
        this.filter=filter;
        if(!StrUtil.isBlank(filter) && !filter.equals("{}")){
            var obj=JSONUtil.toBean(filter, Map.class);
            var objMap=(HashMap<String,String>)obj;
            Map<String,String> map=new HashMap<String,String>();
            for(var item:objMap.entrySet()){
                if(!StrUtil.isAllBlank(item.getKey(),item.getValue())){
                    var key=StrUtil.toSymbolCase(item.getKey().replace("'", "").replace(" ", ""), '_');
                    map.put(key, item.getValue().replace("'", ""));
                }
            }
            this.setFilterMap(map);
        }
    }
    public String getFilter(){
        return this.filter;
    }
}